package net.zzy.pbbot.plugin.tiktok.api;

import lombok.SneakyThrows;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.tiktok.service.TikTokService;
import onebot.OnebotEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

import static net.zzy.pbbot.finalfield.Command.TIK_TOK_SET;

/**
 * @author zhuZhaoYang
 * @date 2021/9/25 23:45
 */
@Component
public class TikTokSet extends BotPlugin {
    private TikTokService tikTokService;

    @Autowired
    public void setTikTokService(TikTokService tikTokService) {
        this.tikTokService = tikTokService;
    }

    @SneakyThrows
    @Override
    public int onPrivateMessage(@NotNull Bot bot, @NotNull OnebotEvent.PrivateMessageEvent event) {
        String rawMessage = event.getRawMessage();
        Pattern pattern = Pattern.compile(TIK_TOK_SET);
        if (!pattern.matcher(rawMessage).find()) {
            return MESSAGE_IGNORE;
        }
        Msg msg = tikTokService.setTikTokUrl(rawMessage);
        msg.sendToFriend(bot, event.getUserId());
        return MESSAGE_BLOCK;
    }

    @SneakyThrows
    @Override
    public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
        String rawMessage = event.getRawMessage();
        if (!rawMessage.endsWith(TIK_TOK_SET)) {
            return MESSAGE_IGNORE;
        }
        Msg msg = tikTokService.setTikTokUrl(rawMessage);
        msg.sendToGroup(bot, event.getGroupId());
        return MESSAGE_BLOCK;
    }
}
